/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QKeyEvent>
#include "query.h"
#include "textedit.h"

using namespace SQL;

TextEdit::TextEdit(QWidget *parent)
		: QTextEdit(parent),m_plainText(false),m_query(0)
{
}

TextEdit::~TextEdit()
{
}

bool TextEdit::isPlainText()
{
	return m_plainText;
}

void TextEdit::setToPlainText(bool plainText)
{
	m_plainText=plainText;
}

const QString & TextEdit::queryName()
{
	return m_queryName;
}

void TextEdit::setQueryName(const QString & queryName)
{
	m_queryName = queryName;
	m_query = defaultDatabase()->query(m_queryName);

	if (!m_query)
		return;
	connect(m_query, SIGNAL(recordChanged(const QSqlRecord&)), SLOT(recordChanged(const QSqlRecord&)));
	connect(m_query, SIGNAL(beforeInsert()), SLOT(setReadWrite()));
	connect(m_query, SIGNAL(beforeUpdate()), SLOT(setReadWrite()));
	connect(m_query, SIGNAL(afterSelect()), SLOT(setReadOnly()));
	connect(m_query, SIGNAL(afterClose()), SLOT(setReadOnly()));
	connect(m_query, SIGNAL(afterPost()), SLOT(setReadOnly()));
	connect(m_query, SIGNAL(afterCancel()), SLOT(setReadOnly()));
	setDisabled(m_query->state() == Query::Select);
	if (m_fieldName.length())
		setFieldName(m_fieldName);
}

const QString & TextEdit::fieldName()
{
	return m_fieldName;
}

void TextEdit::setFieldName(const QString & fieldName)
{
	m_fieldName = fieldName;
	if (m_query)
	{
		if (m_plainText)
			setPlainText(m_query->value(m_fieldName).toString());
		else
			setHtml(m_query->value(m_fieldName).toString());
		setAlignment(Query::typeIsNumber(m_query->value(m_fieldName).type()) ? Qt::AlignRight : Qt::AlignLeft);
	}
}

void TextEdit::setReadWrite()
{
	setEnabled(true);
	if (!m_query || !m_fieldName.length())
		return;
	if (m_query->state()==Query::Insert)
		clear();
	m_query->setValue(m_fieldName,m_plainText?toPlainText():toHtml());
}

void TextEdit::focusInEvent(QFocusEvent * event)
{
	if (m_query && m_query->state() == Query::Update)
		selectAll();
	QTextEdit::focusInEvent(event);
	if (event->isAccepted())
		emit(focusIn());
}

void TextEdit::focusOutEvent(QFocusEvent * event)
{
	QTextEdit::focusOutEvent(event);
	if (event->isAccepted())
		emit(focusOut());
	if (!m_query || !m_fieldName.length())
		return;
	m_query->setValue(m_fieldName,m_plainText?toPlainText():toHtml());
}


void TextEdit::setReadOnly()
{
	setDisabled(true);
	setAlignment(Query::typeIsNumber(m_query->value(m_fieldName).type()) ? Qt::AlignRight : Qt::AlignLeft);
	if (!m_query || !m_fieldName.length())
		return;
	if (m_query->state()==Query::Select)
	{
		if (m_plainText)
			setPlainText(m_query->value(m_fieldName).toString());
		else
			setHtml(m_query->value(m_fieldName).toString());
	}
}

void TextEdit::recordChanged(const QSqlRecord& record)
{
	if (m_plainText)
		setPlainText(record.value(m_fieldName).toString());
	else
		setHtml(record.value(m_fieldName).toString());
}

void TextEdit::bindValue()
{
	if (!m_query || !m_fieldName.length())
		return;
	m_query->setValue(m_fieldName,m_plainText?toPlainText():toHtml());
}

void TextEdit::keyPressEvent(QKeyEvent * event)
{
	if (event->key()==Qt::Key_Escape)
		emit(escapePressed());

	if ((event->key()==Qt::Key_Return || event->key()==Qt::Key_Enter) && event->modifiers()==Qt::ControlModifier)
	{
		bindValue();
		emit(ctrlReturnPressed());
	}
	else
		QTextEdit::keyPressEvent(event);
}
